package com.lianniu.mgr.buyerManage.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.baseService.area.model.AdminAreaVo;
import com.lianniu.mgr.baseService.area.service.IAdminAreaService;
import com.lianniu.mgr.buyerManage.service.IBuyerManageService;
import com.lianniu.mgr.buyerManage.util.BuyerUtils;
import com.lianniu.mgr.buyerManage.vo.BuyerConditionVo;
import com.lianniu.mgr.buyerManage.vo.BuyerVo;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.util.FileUploadUtil;
import com.lianniu.mgr.util.ObjectUtil;

/**
 * Created by dylanliu on 16/7/3.
 */
@Controller
@RequestMapping("/rest/buyerManage")
public class BuyerManageRestController {
  
  private static Logger logger = LoggerFactory.getLogger(BuyerManageRestController.class);
  @Autowired
  IBuyerManageService buyerManageService;
  @Autowired
  FileUploadUtil fileUpload;
  @Autowired
  IAdminAreaService areaService;

  @RequestMapping("/buyerList")
  public @ResponseBody BaseResult buyerList(BuyerConditionVo condition,PaginationInfo paginationInfo, HttpServletRequest request, HttpServletResponse response) {
    logger.info(String.format("query buyerlist,params:condition=[%s],page=[%s]", JSON.toJSONString(condition),JSON.toJSONString(paginationInfo)));
    BaseResult br = buyerManageService.getBuyerListForPage(condition, paginationInfo);
    logger.info(String.format("query buyerlist result:%s", JSON.toJSONString(br)));
    return br;
  }

  @RequestMapping("/buyerApproveList")
  public @ResponseBody BaseResult buyerApproveList(BuyerConditionVo condition,PaginationInfo paginationInfo, HttpServletRequest request, HttpServletResponse response) {
    logger.info(String.format("query buyerApproveList,params:condition=[%s],page=[%s]", JSON.toJSONString(condition),JSON.toJSONString(paginationInfo)));
    BaseResult br = buyerManageService.getBuyerApproveListForPage(condition, paginationInfo);
    logger.info(String.format("query buyerApproveList result:%s", JSON.toJSONString(br)));
    return br;
  }

  @SuppressWarnings("unchecked")
  @RequestMapping("/getBuyer")
  public @ResponseBody BaseResult getBuyer(HttpServletRequest request,ModelMap map) {
	String buyerid = request.getParameter("buyerid");
    logger.info(String.format("get %s buyer", buyerid));
    BaseResult br = buyerManageService.getBuyerById(buyerid);
    
    BuyerVo buyer = (BuyerVo) br.getData();
    ObjectUtil.nvlProperty(buyer);//将null转换为空字符串
    List<AdminAreaVo> provinces =( List<AdminAreaVo>) areaService.getByParentId("1").getData();
    List<AdminAreaVo> citys = ( List<AdminAreaVo>) areaService.getByParentId(buyer.getProvince()).getData();
    List<AdminAreaVo> countrys = (List<AdminAreaVo>) areaService.getByParentId(buyer.getCity()).getData();
    
    map.put("provinces", provinces);
    map.put("citys", citys);
    map.put("countrys", countrys);
    map.put("buyer", buyer);
    logger.info("get buyer result : "+JSON.toJSONString(buyer));
    return BaseResult.success(map);
  }
  
  @RequestMapping("/deleteBuyer")
  public @ResponseBody BaseResult deleteBuyer( String buyerids){
    logger.info(String.format("delete %s buyer", buyerids));
    BaseResult result = buyerManageService.deleteBuyer(buyerids);
    logger.info("delete buyer result ： "+result);
    return result;
  }
  
  @RequestMapping("/checkThroughBuyer")
  public @ResponseBody BaseResult checkThroughBuyer( String buyerids){
    String status = "2";
    logger.info(String.format("check through %s buyer ", buyerids));
    BaseResult result = buyerManageService.checkBuyer(status, buyerids);
    logger.info("check through buyer reuslt :"+result);
    return result;
  }
  
  @RequestMapping("/checkRefuseBuyer/{id}")
  public @ResponseBody BaseResult checkRefuseBuyer(@PathVariable("id") String id){
    String status = "3";
    logger.info(String.format("chech refuse %s buyer", id));
    BaseResult result = buyerManageService.checkBuyer(status, id);
    logger.info("check refuse result : "+ result);
    return result;
  }
  
  @RequestMapping("/checkFrozenBuyer/{id}")
  public @ResponseBody BaseResult checkFrozenBuyer(@PathVariable("id") String id){
    String status = "4";
    logger.info(String.format("frozen buyer %s ", id));
    BaseResult result = buyerManageService.checkBuyer(status, id);
    logger.info("frozen buyer result : "+ id);
    return result;
  }
  
  @RequestMapping("/updateBuyer")
  public @ResponseBody BaseResult updateBuyer(BuyerVo buyer){
    buyer.setBuyerStatus(BuyerUtils.BUYER_STATUS_1);
    buyerManageService.updateBuyer(buyer);
    return BaseResult.success();
  }

  @RequestMapping("/modifyBuyer")
  public @ResponseBody BaseResult modifyBuyer(BuyerVo buyer){
    buyer.setBuyerStatus(BuyerUtils.BUYER_STATUS_1);
    buyerManageService.modifyBuyer(buyer);
    return BaseResult.success();
  }
  
}
